package com.nue.dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.nue.po.User;
import com.nue.util.DBUtil;

public class UserDaoImpl implements UserDao{

	@Override
	public int insert(User user) throws ClassNotFoundException, SQLException {
		String sql = "insert into user values(null,?,?,?)";
		Object[] params = {user.getUsername(),user.getPassword(),user.getBirthday()};
		return  DBUtil.executeUpdate(sql,params);
		
	}

	@Override
	public User login(String username, String password) throws ClassNotFoundException, SQLException {
        String sql = "select * from user where username = ? and password = ?";
		
		Connection connection = DBUtil.getConnection();
		
		ResultSet rs = DBUtil.executeQuery(connection, sql, username,password);
		
		User user = null;
		
		int id;
		Date birthday;
		
		if(rs.next()) {
			id = rs.getInt("id");
			username = rs.getString("username");
			password = rs.getString("password");
			birthday = rs.getDate("birthday");
			
			user = new User(id,username,password,birthday);
		}
		
		DBUtil.closeConnection(connection);
		return user;
	}

	@Override
	public int updatePassword(String oldpassword, String newpassword, int id)
			throws ClassNotFoundException, SQLException {
		
		String sql = "select * from user where id = ? and password = ?";
		
		Connection connection = DBUtil.getConnection();
		ResultSet rs = DBUtil.executeQuery(connection, sql, id,oldpassword);
		int n = 0;
		if(rs.next()) {
			
			sql = "update user set password = ? where id = ?";
			
			n = DBUtil.executeUpdate(sql, newpassword,id);
		}
		
		DBUtil.classConnection(connection);
		
		return n;
	}

	@Override
	public List<User> getAll() throws ClassNotFoundException, SQLException {
        String sql = "select * from user";
		
		Connection connection = DBUtil.getConnection();
		
		ResultSet rs = DBUtil.executeQuery(connection, sql);
		
		User user = null;
		
		String username;
		String password;
		Date birthday;
		int id;
		
		List<User> list = new ArrayList<User>();
		
		while(rs.next()) {
			id = rs.getInt("id");
			username = rs.getString("username");
			password = rs.getString("password");
			birthday = rs.getDate("birthday");
			
			user = new User(id,username,password,birthday);
			
			list.add(user);
		}
		
		DBUtil.closeConnection(connection);
		return list;
		
	}

	@Override
	public User getById(int id) throws ClassNotFoundException, SQLException {
		String sql = "select * from user where id = ?";
		
		Connection connection = DBUtil.getConnection();
		
		ResultSet rs = DBUtil.executeQuery(connection, sql, id);
		
		User user = null;
		
		String username;
		String password;
		Date birthday;
		
		if(rs.next()) {
			username = rs.getString("username");
			password = rs.getString("password");
			birthday = rs.getDate("birthday");
			
			user = new User(id,username,password,birthday);
		}
		
		DBUtil.closeConnection(connection);
		return user;
	}

	@Override
	public int delete(int id) throws ClassNotFoundException, SQLException {
		String sql = "delete from user where id = ?";
		
		return DBUtil.executeUpdate(sql, id);
	}

}
